﻿using UnityEngine;
using System.Collections;

namespace Common
{
	//帧率函数挂接在游戏对象的任何物体上都OK
	public class FPS : MonoBehaviour
	{
		
		public float f_UpdateInterval = 0.5F;//声明变量更新时间间隔并赋值

        private float f_LastInterval;//声明变量上一间隔

        private int i_Frames = 0;//声明变量帧数并赋值

        private float f_Fps;//声明变量帧率
		
		void Start()
		{
            f_LastInterval = Time.realtimeSinceStartup;//游戏开始的几秒钟内的实时时间
			
			i_Frames = 0;
			
		}
		
		void OnGUI()
		{
			GUI.Label(new Rect(0, 200, 100, 100), "FPS:" + f_Fps.ToString("f2"));//GUI图标位置为（0,200）图标大小为（100,100）	,	GUI显示文本	
		}
		
		void Update()
		{
			++i_Frames;
			
			if (Time.realtimeSinceStartup > f_LastInterval + f_UpdateInterval)
			{
				f_Fps = i_Frames / (Time.realtimeSinceStartup - f_LastInterval);
				
				i_Frames = 0;
				
				f_LastInterval = Time.realtimeSinceStartup;
			}
		}
	}
}
